<template>
	<view class="common-bg">
		<!-- #ifdef APP -->
		<uni-nav-bar :border="false" color="#222" backgroundColor="transparent" title="" />
		<!-- #endif -->
		<uni-nav-bar :border="false" color="#222" backgroundColor="transparent" left-icon="left" leftText="" 
			:title="$t('addDevice.title')" @clickLeft="clickLeft" />
		<view class="pad-20">
			<view class="step-box">
				<view class="line line-one"></view>
				<view class="line line-two"></view>
				<view class="item on item-one">
					<view class="y">
						1
					</view>
					<view class="t">
						<!-- 搜索设备 -->
						{{$t('addDevice.searchDevices')}}
					</view>
				</view>
				<view class="item item-one" :class="{on : statusIndex === 1 || statusIndex === 2}">
					<view class="y">
						2
					</view>
					<view class="t">
						<!-- WiFi配置 -->
						{{$t('addDevice.wiFiConfiguration')}}
					</view>
				</view>
				<view class="item item-one" :class="{on : statusIndex === 2}">
					<view class="y">
						3
					</view>
					<view class="t">
						<!-- 完成 -->
						{{$t('addDevice.done')}}
					</view>
				</view>
			</view>
			<stepOne v-if="statusIndex == 0" @doConcat="doConcat" @deviceSelected="onDeviceSelected" />
			<stepTwo v-if="statusIndex == 1" @doConcat="doConcat" :selectedDevice="selectedDevice" />
			<stepThree v-if="statusIndex == 2" @doConcat="doConcat" />

		</view>
	</view>
</template>

<script>
	import stepOne from './components-step/step-one.vue'
	import stepTwo from './components-step/step-two.vue'
	import stepThree from './components-step/step-three.vue'
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue';
	export default {
		data() {
			return {
				statusIndex: 0,
				selectedDevice: null  // 存储从步骤1选中的设备信息
			}
		},
		components: {
			stepOne,
			stepTwo,
			stepThree,
			uniNavBar
		},
		
		onUnload() {
			// 页面卸载时关闭蓝牙适配器
			console.log('[ADD-DEVICE] 页面卸载，关闭蓝牙适配器')
			this.closeBluetooth()
		},
		
		methods: {
			clickLeft() {
				uni.navigateBack(-1)
			},
			doConcat(status) {
				this.statusIndex = status;
			},
			/**
			 * 处理设备选中事件
			 */
			onDeviceSelected(deviceInfo) {
				console.log('[ADD-DEVICE] 设备已选中:', deviceInfo)
				this.selectedDevice = deviceInfo
			},
			
			/**
			 * 关闭蓝牙适配器
			 */
			closeBluetooth() {
				uni.closeBluetoothAdapter({
					success: () => {
						console.log('[ADD-DEVICE] 蓝牙适配器已关闭')
					},
					fail: (error) => {
						console.log('[ADD-DEVICE] 关闭蓝牙适配器失败:', error)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.step-box {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 40rpx auto 0;
		position: relative;

		.line {
			position: absolute;
			width: 160rpx;
			height: 4rpx;
			background-color: rgba(102, 102, 102, 0.08);
			bottom: 56rpx;

			&.line-one {
				left: 130rpx;
			}

			&.line-two {
				right: 130rpx;
			}
		}

		.item {
			width: 100rpx;
			text-align: center;

			.y {
				width: 68rpx;
				height: 68rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 100%;
				background-color: rgba(102, 102, 102, 0.08);
				color: rgba(0, 0, 0, 0.40);
				font-size: 28rpx;
				margin: 0 auto;
			}

			.t {
				margin-top: 16rpx;
				text-align: center;
				font-size: 22rpx;
				color: rgba(0, 0, 0, 0.40);
			}

			&.on {
				.y {
					background-color: rgba(0, 0, 0, 0.80);
					color: #fff;
				}

				.t {
					color: rgba(0, 0, 0, 0.80);
				}
			}
		}
	}

	@keyframes ping {
		0% {
			transform: translate(-50%, -50%) scale(1);
			opacity: 1;
		}

		75%,
		100% {
			transform: translate(-50%, -50%) scale(1.7);
			opacity: 0;
		}
	}

	.animate-ping {
		animation: ping 1s cubic-bezier(0, 0, 0.2, 1) infinite;
	}
</style>